package com.cf.service.impl;


import com.cf.dao.AddressDao;
import com.cf.dao.UserDao;
import com.cf.entity.Address;
import com.cf.entity.User;
import com.cf.service.AddressService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AddressServiceImpl implements AddressService {
    @Autowired
    private AddressDao addressDao;
    @Autowired
    private UserDao userDao;


    @Override
    public PageInfo<Address> selectUserAddress(int pagenum,int pagesize,long userid) {
        PageHelper.startPage(pagenum,pagesize);
        List<Address> addresses = addressDao.selectUserAddress(userid);
        if(addresses!=null){
            for(Address a :addresses){
                User user = userDao.selectUserById(userid);
                a.setUsername(user.getUsername());
            }
        }
        PageInfo<Address> addressPageInfo =new PageInfo<>(addresses);
        return addressPageInfo;
    }

    @Override
    public Address selectByAddressid(long addressid) {
        return addressDao.selectByAddressid(addressid);
    }

    @Override
    public int updateAddress(Address address) {
        return addressDao.updateAddress(address);
    }

    @Override
    public int deleteAddress(long addressid) {
        return addressDao.deleteAddress(addressid);
    }

    @Override
    public int addAddress(Address address) {
        int i = addressDao.addAddress(address);

        return i;
    }

    @Override
    public List<Address> selectAllAddress(long userid) {
        return addressDao.selectAllAddress(userid);
    }


}
